package com.marktrace.controller;

import com.alibaba.fastjson.JSON;
import com.marktrace.service.LocationService;
import com.marktrace.util.AESUtil;
import com.marktrace.util.DateUtils;
import com.marktrace.util.RPC;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api")
@Slf4j
public class RPCInterfaceController extends BaseController {


    @Autowired
    private LocationService locationService;

    /**
     * 按车牌号和时间获取校车学员信息数据
     * @param query {licensePlate,plateColor,startTime,endTime}
     * @return
     */
    @RequestMapping("/queryStudentInfo")
    @ResponseBody
    public Object queryStudentInfo(String query){
        RPC rpc;
        try {
            query = query.replaceAll(" ","+");
            String conditionStr = AESUtil.inBase64(query) ;
            Map<String,Object> paramMap = JSON.parseObject(conditionStr);
            String licensePlate = paramMap.get("licensePlate").toString();
            Map<String, Object> studentParam = new HashMap<>();
            studentParam.put("address",licensePlate);
            studentParam.put("starttime",paramMap.get("startTime"));
            studentParam.put("endtime",paramMap.get("endTime"));
            List<Map<String,Object>> dataList = locationService.rpcQueryInfo(studentParam);

            LinkedHashMap<String, Object> resultMap = new LinkedHashMap<>();
            resultMap.put("licensePlat",licensePlate);
            resultMap.put("plateColor",paramMap.get("plateColor"));
            resultMap.put("dateTime", DateUtils.getDateTime());
            resultMap.put("data",dataList);
            rpc = RPC.ok("查询成功").put("data",resultMap);
            log.info("调用接口成功");
            return AESUtil.outBase64(JSON.toJSONString(rpc));
        }catch (Exception e){
            log.info("调用接口出错:"+e.getMessage());
            rpc = RPC.illegal();
            return AESUtil.outBase64(rpc.toString());
        }

    }
}
